
#ifndef __AUTO_LOCK_READ_H
#define __AUTO_LOCK_READ_H

#include "APRRWMutex.h"

/** An APRRWMutex automatic locking wrapper class for a read/write
 * mutex. Destroying the wrapped mutex afterwards is to be handled
 * elsewhere. */
class APRAutoLockRead
{
   public:
      /** Lock the read/write mutex (for reading, implying a reader is
       * accessing the resource).
       * 
       * @param i_rwmutex A pointer to a created APRRWMutex that is
       * unlocked. */
      APRAutoLockRead(APRRWMutex *i_rwmutex)
      {
         _rwmutex = i_rwmutex;
         _rwmutex->lockRead();
      }
      /** Unlock the read/write mutex (the reader has finished
       * accessing this resource). */
      ~APRAutoLockRead()
      {
         if (_rwmutex) _rwmutex->unlock();
      }
      /** This removes the lock and turns off the mutex before the
       * destructor. */
      void turnOff()
      {
         _rwmutex->unlock();
         _rwmutex = NULL;
      }
   private:
      /** A pointer to the APR read/write mutex that is to be used. */
      APRRWMutex *_rwmutex;
};

#endif
